import { _decorator, Component, Node, Sprite, Vec3 } from 'cc';
import { GlobalData } from '../../../Data/GlobalData';
const { ccclass, property } = _decorator;

@ccclass('ZumaClearMajiang')
export class ZumaClearMajiang extends Component {

    //麻将名称
    public MajiangName: string = "";

    //麻将
    @property(Sprite)
    private MajiangSprite: Sprite = null;

    //移动索引
    public MoveIndex: number = 0;
    //后退速度
    public BackSpeed: number = 0;
    //当前已经移动的位置
    public CurrentDistance: number = 0;


    /**
     * 更新麻将
     */
    public UpdateMajiang(_name: string) {
        this.MoveIndex = 0;
        this.CurrentDistance = 0;
        this.MajiangName = _name;
        this.MajiangSprite.spriteFrame = app.resManager.SpriteFrames[_name];
    }

    /**
         * 自己移动
         */
    public MoveSelf(_dt: number) {
        if (this.CurrentDistance >= app.pathManager.MaxDistance) {//游戏结束
            // this.DragonLoad.GameScene.GameOver();
            return;
        }
        this.CurrentDistance += (GlobalData.MajiangMoveSpeed + GlobalData.BodyAddSpeed) * _dt;
        this.MoveFollow(app.pathManager.findIndexByDistance(this.CurrentDistance));
    }

    /**
    * 更新位置和旋转
    */
    public UpdateTransform(_pos: Vec3, _angle: number) {
        this.node.setPosition(_pos.x, _pos.y);
        const degrees = _angle * 180 / Math.PI;
        this.node.angle = degrees;
    }

    /**
    * 更新距离
    */
    public UpdateDistance(_distance: number) {
        this.CurrentDistance = _distance;
        if (this.CurrentDistance <= 0) {
            this.CurrentDistance = 0;
        }
        this.MoveFollow(app.pathManager.findIndexByDistance(this.CurrentDistance));
    }

    /**
    * 后退
    */
    public MoveBack(_dt: number) {
        this.CurrentDistance -= this.BackSpeed * _dt;
        if (this.CurrentDistance <= 0) {
            return;
        }
        this.MoveFollow(app.pathManager.findIndexByDistance(this.CurrentDistance));

    }

    /**
    * 我跟随移动
    */
    public MoveFollow(_moveindex: number) {
        if (_moveindex < 0) {
            // this.UpdateTransform(new Vec3(1000, 1000), 0);
            return;
        }

        //更新我移动的索引
        this.MoveIndex = _moveindex;

        // 获取当前位置和角度
        const entry = app.pathManager.DistanceTable[this.MoveIndex];
        const curve = app.pathManager.Cuvers[entry.curveIndex];
        const position = app.pathManager.getPointOnCurve(curve, entry.t);
        const angle = app.pathManager.getTangentAngle(curve, entry.t);
        this.UpdateTransform(new Vec3(position.x, position.y), angle);
    }




}


